package cn.edu.zsc.blms.service.impl.collection;

import cn.edu.zsc.blms.App;
import cn.edu.zsc.blms.domain.Book;
import cn.edu.zsc.blms.exception.EntityNotExistException;
import cn.edu.zsc.blms.service.BookService;
import cn.edu.zsc.blms.util.DateUtil;

import java.text.ParseException;
import java.util.Date;
import java.util.Set;

/**
 * 图书Service
 *
 *
 */
public class BookServiceImpl extends BaseServiceImpl<Book> implements BookService {


    /**
     * 单例
     */
    private static BookService singleton;

    /**
     * 私有构造法
     */
    private BookServiceImpl() {

    }

    /**
     * 获得单例，非线程安全
     *
     * @return BookService单例
     */
    public static BookService getSingleton() {
        if (singleton == null) {
            singleton = new BookServiceImpl();
        }
        return singleton;
    }


    /**
     * ID计数器
     */
    private int idCounter;


    public void create() {
        if (idCounter >= 100) {
            System.out.println("图书数量已满，不能再添加");
            return;
        }
        System.out.print("请输入书名:");
        String name = App.input.nextLine();
        System.out.print("请输入书号:");
        String isbn = App.input.nextLine();
        System.out.print("请输入类目:");
        String category = App.input.nextLine();
        System.out.print("请输入作者:");
        String author = App.input.nextLine();
        System.out.print("请输入出版社:");
        String press = App.input.nextLine();
        System.out.print("请输入出版日期，格式为yyyy-MM-dd：");
        Date publishDate = null;
        while (publishDate == null) {
            String strDate = App.input.nextLine();
            try {
                publishDate = DateUtil.parse(strDate);
            } catch (ParseException e) {
                System.out.print("输入的日期格式错误，请重写输入:");
            }
        }
        System.out.print("请输入单价:");
        Double price = null;
        while (price == null) {
            String strPrice = App.input.nextLine();
            try {
                price = Double.parseDouble(strPrice);
            } catch (NumberFormatException e) {
                System.out.print("输入的单价格式错误，请重写输入:");
            }
        }
        Book book = new Book(idCounter, name, isbn, category, author, press, publishDate, price);
        DATA.put(book.id,book);
        System.out.println("成功增加1本图书!");
        idCounter++;
    }


    /**
     * 废弃图书
     */
    @Override
    public void disable() {
        System.out.print("请输入要废弃的图书ID:");
        Book book = null;
        try {
            book = getOne();
        } catch (EntityNotExistException e) {
            System.out.println(e.getMessage());
        }
        if (book != null) {
            book.disabled = true;
            System.out.println("该图书已被废弃");
        }
    }

    public void update() {
        System.out.print("请输入要编辑的图书ID:");
        Book book = null;
        try {
            book = getOne();
        } catch (EntityNotExistException e) {
            System.out.println(e.getMessage());
        }
        if (book != null) {
            System.out.print("请输入书名，直接按回车表示不修改:");
            String name = App.input.nextLine();
            book.name = "".equals(name) ? book.name : name;
            System.out.print("请输入书号，直接按回车表示不修改:");
            String isbn = App.input.nextLine();
            book.isbn = "".equals(isbn) ? book.isbn : name;
            System.out.print("请输入类目，直接按回车表示不修改:");
            String category = App.input.nextLine();
            book.category = equals(category) ? book.category : category;
            System.out.print("请输入作者，直接按回车表示不修改:");
            String author = App.input.nextLine();
            book.author = equals(author) ? book.author : author;
            System.out.print("请输入出版社，直接按回车表示不修改:");
            String press = App.input.nextLine();
            book.press = equals(press) ? book.press : press;
            System.out.print("请输入出版日期，格式为yyyy-MM-dd，直接按回车表示不修改:");
            Date publishDate = null;
            while (publishDate == null) {
                String strDate = App.input.nextLine();
                if ("".equals(strDate)){
                    publishDate = book.publishDate;
                    break;
                }
                try {
                    publishDate = DateUtil.parse(strDate);
                } catch (ParseException e) {
                    System.out.print("输入的日期格式错误，请重写输入:");
                }
            }
            book.publishDate = publishDate;
            System.out.print("请输入单价:");
            Double price = null;
            while (price == null) {
                String strPrice = App.input.nextLine();
                if ("".equals(strPrice)){
                    price = book.price;
                    break;
                }
                try {
                    price = Double.parseDouble(strPrice);
                } catch (NumberFormatException e) {
                    System.out.print("输入的单价格式错误，请重写输入:");
                }
            }
            // TODO: 暂不对输入的单价做数据格式检测
            book.price = App.input.nextDouble();
            App.input.nextLine();
            System.out.println("该图书的信息已被修改");
        }
    }

    /**
     * 查询图书
     */
    @Override
    public void query() {
        System.out.print("请输入要查询的图书名称:");
        String name = App.input.nextLine();
        Set<Integer> ids = DATA.keySet();
        // TODO: 根据图书名称模糊查询，并打印查询结果，暂且打印所有图书信息
        for (Integer id : ids) {
            Book book = DATA.get(id);
            if (book.name != null && book.name.contains(name)) {
                System.out.println(book);
            }
        }

    }
}
